$(document).ready(function(){

	//LOGO LINK
	$("#logo").click(function(){
		window.location.href = "/";
	});
	
	//MAKE SIDEBAR SAME SIZE AS PAGE, IF PAGE IS LARGER
	var sideBarHeight = parseInt($("#sidebar").outerHeight(true));
	var pageHeight = parseInt($("#page").outerHeight(true));
	
	if(sideBarHeight < pageHeight)
	{
		$("#sidebar").css('height', pageHeight);
	}

	//ACCOUNT OPTIONS TOGGLE
	var accountOptionsOpen = false;
	
	$(document).click(function(event) {
		if(accountOptionsOpen == true)
		{
			if((event.target.id == "dd_1")||(event.target.id == "dd_2")||(event.target.id == "dd_3")||(event.target.id == "dd_4")||(event.target.id == "dd_4"))
			{
				return false;
			}
			else
			{
				if(event.target.id != "topButton")
				{
					accountOptionsOpen = false;
					$("#dropDown").hide();
				}
			}
		}
    });
	
	$(".topButton").click(function()
	{
		$("#dropDown").toggle(0, function()
		{
			if(accountOptionsOpen == true)
			{
				accountOptionsOpen = false;
			}
			else
			{
				accountOptionsOpen = true;
			}
		});
		
	});
	
	//SEARCH FORM ACTION
	$("#search-submit").click(function()
	{
		var q = $("#search-text").val().trim();
		
		if(q != "")
		{
			if(searching == true)
			{
				window.location.href = "http://" + window.location.host + "/search.php?q=" + q;
			}
		}
	});
	
	$("#search-text").keypress(function(e)
	{
		if(e.keyCode == 13)
		{
			var q = $("#search-text").val().trim();
		
			if(q != "")
			{
				if(searching == true)
				{
					window.location.href = "http://" + window.location.host + "/search.php?q=" + q;
				}
			}
		}
	});
	
	var searching = false;
	
	$("#search-text").focus(function()
	{
		searching = true;
	
		if($("#search-text").val() == "Search")
		{	
			$("#search-text").val("");
		}
		return false;
	});
	
	$("#search-text").blur(function()
	{
		if($("#search-text").val() == "")
		{
			searching = false;
			$("#search-text").val("Search");
		}
		else
		{
			searching = true;
		}
		return false;
	});

});

//POPUP
/*
	header = Message to display in popup header (string)
	content = file name to get content from (string)
	decisionType = determines what button(s) will be displayed (int)
			0 = No Buttons
			1 = OK
			2 = Confirm | Cancel
			3 = Yes | No | Cancel
*/

function Popup(header, content, decisionType){

	// Remove current popup
	$('#popup').remove();
	$('#blackout').remove();
	
	// Create popup
	$('body').append("<div id='blackout'></div>");
	$('#wrapper').append("<div id='popup'></div>");
	$('#popup').append("<div id='popupHeader'>" + header + "</div>");
	$('#popup').append("<div id='popupContent'><div id='loadingPopup'></div></div>");
	$('#popup').append("<div id='popupButtons'><div id='backToLink'></div></div>");
	
		switch(decisionType){
			case 0:
				$('#popupButtons').append("<input type='button' class='button cancelPopup' value='Cancel' />");
				
				$(".cancelPopup").click(function(){
					$("#popup").remove();
					$('#blackout').remove()
				});

			break;
			case 1:
				$('#popupButtons').append("<input type='button' class='button' value='OK' />");
			break;
			case 2:
				$('#popupButtons').append("<input type='button' class='button' value='Confirm' /><input type='button' class='button cancelPopup' value='Cancel' />");
			break;
			case 3:
				$('#popupButtons').append("<input type='button' class='button' value='Yes' /><input type='button' class='button' value='No' /><input type='button' class='button cancelPopup' value='Cancel' />");
			break;	
		}
		
		/* BUTTON STYLES */
		$(".cancelPopup").css({
			"background-color" : "#777777",
			"color" : "#FFFFFF",
			"border-color" : "#666666"
		});

	// Display popup
	$("#popup").show();
	
	// Get popup content
	$.ajax({
		type: "POST",
		url: "/page/popup/" + content + ".php",
		success: function(obj)
		{
			$("#loadingPopup").remove();
			$("#popupContent").html(obj);
		}
	});
	
}